{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 活动热度数据（event_attendees.csv）处理\n",
    "（只取训练集和测试集中出现的用户ID）\n",
    "\n",
    "数据来源于Kaggle竞赛：Event Recommendation Engine Challenge，根据\n",
    "events they’ve responded to in the past\n",
    "user demographic information\n",
    "what events they’ve seen and clicked on in our app\n",
    "用户对某个活动是否感兴趣\n",
    "\n",
    "竞赛官网：\n",
    "https://www.kaggle.com/c/event-recommendation-engine-challenge/data\n",
    "\n",
    "\n",
    "event_attendees.csv文件：共5维特征\n",
    "event_id：活动ID\n",
    "yes, maybe, invited, and no：以空格隔开的用户列表，\n",
    "分别表示该活动参加的用户、可能参加的用户，被邀请的用户和不参加的用户."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import numpy as np\n",
    "import scipy.sparse as ss\n",
    "import scipy.io as sio\n",
    "\n",
    "#保存数据\n",
    "import pickle\n",
    "\n",
    "from sklearn.preprocessing import normalize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "总的用户数目超过训练集和测试集中的用户，\n",
    "为节省处理时间和内存，先去处理train和test，得到竞赛需要用到的事件和用户\n",
    "然后对在训练集和测试集中出现过的事件和用户建立新的ID索引\n",
    "先运行user_event.ipynb,\n",
    "得到事件列表文件：PE_userIndex.pkl"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取之前算好的测试集和训练集中出现过的活动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of events in train & test :13418\n"
     ]
    }
   ],
   "source": [
    "#读取训练集和测试集中出现过的事件列表\n",
    "eventIndex = pickle.load(open(\"PE_eventIndex.pkl\", 'rb'))\n",
    "n_events = len(eventIndex)\n",
    "\n",
    "print(\"number of events in train & test :%d\" % n_events)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# user_friends.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "#读取数据\n",
    "\"\"\"\n",
    "  统计某个活动，参加和不参加的人数，计算活动热度\n",
    "\"\"\"\n",
    "\n",
    "#活动活跃度\n",
    "eventPopularity = ss.dok_matrix((n_events, 1))\n",
    "    \n",
    "f = open(\"event_attendees.csv\", 'r')\n",
    "\n",
    "#字段：event_id,yes, maybe, invited, and no\n",
    "f.readline() # skip header\n",
    "\n",
    "for line in f:\n",
    "    cols = line.strip().split(\",\")\n",
    "    eventId = str(cols[0])   #event_id\n",
    "    #if eventIndex.has_key(eventId):\n",
    "    if eventId in eventIndex:\n",
    "        i = eventIndex[eventId]  #事件索引\n",
    "        \n",
    "        #yes - no\n",
    "        eventPopularity[i, 0] = \\\n",
    "          len(cols[1].split(\" \")) - len(cols[4].split(\" \"))\n",
    "    \n",
    "f.close()\n",
    "    \n",
    "eventPopularity = normalize(eventPopularity, norm=\"l1\",\n",
    "      axis=0, copy=False)\n",
    "sio.mmwrite(\"EA_eventPopularity\", eventPopularity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 3.22072305e-05],\n",
       "        [-9.20206586e-06],\n",
       "        [-1.15025823e-05],\n",
       "        ...,\n",
       "        [ 5.52123952e-05],\n",
       "        [-5.65927051e-04],\n",
       "        [ 4.60103293e-06]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eventPopularity.todense()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  (7707, 0)\t3.6808263455145683e-05\n",
      "  (2036, 0)\t6.211394458055834e-05\n",
      "  (8189, 0)\t-6.901549397839815e-06\n",
      "  (9916, 0)\t4.831084578487871e-05\n",
      "  (5710, 0)\t1.380309879567963e-05\n",
      "  (7348, 0)\t6.441446104650494e-05\n",
      "  (8529, 0)\t0.0002277511301287139\n",
      "  (12260, 0)\t6.441446104650494e-05\n",
      "  (805, 0)\t-2.9906714057305866e-05\n",
      "  (11676, 0)\t4.6010329318932104e-06\n",
      "  (2227, 0)\t3.220723052325247e-05\n",
      "  (6790, 0)\t1.6103615261626235e-05\n",
      "  (13062, 0)\t2.0704648193519445e-05\n",
      "  (3509, 0)\t1.6103615261626235e-05\n",
      "  (10963, 0)\t0.0005107146554401463\n",
      "  (11986, 0)\t4.3709812852985494e-05\n",
      "  (9844, 0)\t6.901549397839815e-06\n",
      "  (357, 0)\t1.6103615261626235e-05\n",
      "  (1901, 0)\t2.9906714057305866e-05\n",
      "  (3337, 0)\t1.380309879567963e-05\n",
      "  (6002, 0)\t-8.741962570597099e-05\n",
      "  (8078, 0)\t6.901549397839815e-05\n",
      "  (13255, 0)\t1.1502582329733024e-05\n",
      "  (3424, 0)\t2.300516465946605e-05\n",
      "  (6636, 0)\t3.220723052325247e-05\n",
      "  :\t:\n",
      "  (12616, 0)\t2.3005164659466052e-06\n",
      "  (4100, 0)\t1.8404131727572842e-05\n",
      "  (1764, 0)\t1.380309879567963e-05\n",
      "  (5479, 0)\t0.00022545061366276728\n",
      "  (6841, 0)\t-1.6103615261626235e-05\n",
      "  (11922, 0)\t1.1502582329733024e-05\n",
      "  (9321, 0)\t0.0003266733381644179\n",
      "  (9200, 0)\t1.1502582329733024e-05\n",
      "  (6330, 0)\t3.450774698919907e-05\n",
      "  (12612, 0)\t4.6010329318932104e-06\n",
      "  (4326, 0)\t4.831084578487871e-05\n",
      "  (5039, 0)\t2.0704648193519445e-05\n",
      "  (113, 0)\t-0.0003151707558346849\n",
      "  (995, 0)\t-0.0004048908980066025\n",
      "  (2857, 0)\t1.6103615261626235e-05\n",
      "  (4711, 0)\t1.8404131727572842e-05\n",
      "  (6318, 0)\t-5.751291164866513e-05\n",
      "  (3030, 0)\t4.831084578487871e-05\n",
      "  (5791, 0)\t0.0001380309879567963\n",
      "  (4359, 0)\t9.202065863786421e-06\n",
      "  (9174, 0)\t-2.3005164659466052e-06\n",
      "  (2929, 0)\t1.6103615261626235e-05\n",
      "  (5353, 0)\t2.5305681125412656e-05\n",
      "  (8360, 0)\t2.0704648193519445e-05\n",
      "  (2803, 0)\t1.1502582329733024e-05\n"
     ]
    }
   ],
   "source": [
    "print(eventPopularity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
